package com.yang.booking.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.booking.dao.ItemMapper;
import com.yang.booking.dao.StaffItemMapper;
import com.yang.booking.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private StaffItemMapper staffItemMapper;

    public Map<String, Object>  insertItem(String storeId, String no, String name, String typeId,
                                           Float price, int duration, String pictureList, String thumbnail, String videoList,
                                            String details, int shelf, int appointment){


        Item item = new Item();
        item.setStoreId(storeId);
        item.setNo(no);
        item.setName(name);
        item.setTypeId(typeId);
        item.setPrice(price);
        item.setDuration(duration);
        item.setPictureList(pictureList);
        item.setThumbnail(thumbnail);
        item.setVideoList(videoList);
        item.setDetails(details);
        item.setShelf(shelf);
        item.setAppointment(appointment);
        item.setCreateAt(new Date());
        item.setUpdateAt(new Date());
        itemMapper.insert(item);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "插入成功");
        msg.put("code", 200);
        Map<String, Object> data = new HashMap<String, Object>();
        msg.put("data", data);

        return msg;
    }

    public Map<String, Object>  queryItemAll(String storeId, String nameOrNo, String typeId, String shelf, String appointment, String pageSize, String pageNo){

        QueryWrapper<Item> wrapper = new QueryWrapper<>();

        if (!nameOrNo.equals("")){
            wrapper.likeRight("name", nameOrNo).or().likeLeft("name", nameOrNo).or().like("name",nameOrNo).or().
                    likeRight("no", nameOrNo).or().likeLeft("no", nameOrNo).or().like("no",nameOrNo);
        }

        if (!typeId.equals("")){
            wrapper.eq("type_id", typeId);
        }

        if (!shelf.equals("")){
            wrapper.eq("shelf", shelf);
        }

        if (!appointment.equals("")){
            wrapper.eq("appointment", appointment);
        }
        wrapper.eq("store_id", storeId);
        Page<Item> page = new Page<>(Long.parseLong(pageNo),Long.parseLong(pageSize));
        itemMapper.selectPage(page, wrapper);
        List<Item> itemList = page.getRecords();

        int count = 0;
        for (Item i: itemList) {
            if (i.getAppointment() == 1){
                count += 1;
            }
        }

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "查询成功");
        msg.put("code", 200);
        Map<String, Object> data = new HashMap<String, Object>();
        msg.put("data", data);
        data.put("rows", itemList);
        data.put("total", itemList.size());
        data.put("count", count);

        return msg;
    }

    public Map<String, Object> updateItemAppointment(String itemIdList, String appointment){

        for (String itemId: itemIdList.split(",")) {
            Item i = new Item();
            i.setId(itemId);
            i.setAppointment(Integer.parseInt(appointment));
            itemMapper.updateById(i);
        }

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "更新成功");
        msg.put("code", 200);
        return msg;
    }

    public Map<String, Object> queryStaffItem(String staffId, String nameOrNo, String typeId, String select, String pageSize, String pageNo){

        QueryWrapper<Item> wrapper = new QueryWrapper<>();

        if (!nameOrNo.equals("")){
            wrapper.likeRight("name", nameOrNo).or().likeLeft("name", nameOrNo).or().like("name",nameOrNo).or().
                    likeRight("no", nameOrNo).or().likeLeft("no", nameOrNo).or().like("no",nameOrNo);
        }

        if (!typeId.equals("")){
            wrapper.eq("type_id", typeId);
        }

        wrapper.eq("appointment", 1);
        Page<Item> page = new Page<>(Long.parseLong(pageNo),Long.parseLong(pageSize));
        itemMapper.selectPage(page, wrapper);
        List<Item> itemList = page.getRecords();

        Iterator<Item> itemIterator = itemList.iterator();
        while (itemIterator.hasNext()) {
            Item item = itemIterator.next();
            QueryWrapper<StaffItem> siwrapper = new QueryWrapper<>();
            siwrapper.eq("staff_id", staffId).eq("item_id", item.getId());
            if (staffItemMapper.selectOne(siwrapper) != null){
                item.setBelonging(true);
                if(select.equals("2")){
                    itemIterator.remove();
                    }
            }else {
                if(select.equals("1")){
                    itemIterator.remove();
                }
            }
        }

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "查询成功");
        msg.put("code", 200);
        Map<String, Object> data = new HashMap<String, Object>();
        msg.put("data", data);
        data.put("rows", itemList);
        data.put("total", itemList.size());
        return msg;
    }

}
